'use strict';
angular.module('app.controllers')
    .controller('TaskCtrl', function($scope, $state, $http, $window, PreopenService, NgTableParams, $filter) {
        var vm = this;
        vm.hotelName = $window.sessionStorage.hotelName;
        vm.hotelId = $window.sessionStorage.hotelId;
        vm.statusOptions = ['Completed', 'In Progress', 'Major Issues', 'Not Started Yet']
        vm.opendate = new Date();
        vm.updateBy = '';
        vm.editable = true;
        vm.loading = false;
        vm.load = function() {
            vm.loading = true;


            PreopenService.taskGet($window.sessionStorage.hotelId).then(function(ret) {
                vm.tasks = ret.data.value.tasks;
                vm.lastUpdated = ret.data.value.addDate;
                vm.taskObj = ret.data.value;
                vm.opendate = ret.data.value.openDate == null ? new Date() : new Date(ret.data.value.openDate);
                vm.updateBy = ret.data.value.updateBy;
                PreopenService.allTask().then(function(ret) {
                    vm.result = ret.data.value;
                    vm.tableParams = new NgTableParams({
                        count: 25
                    }, {
                        dataset: vm.merge(),
                        counts: []
                    });
                })
                PreopenService.completionGet($window.sessionStorage.hotelId).then(function(ret) {
                    vm.completion = ret.data.value;
                    vm.loading = false;
                })
            });



        }
        vm.merge = function() {

            angular.forEach(vm.tasks, function(value2, key2) {
                angular.forEach(vm.result, function(value, key) {
                    if (value2.no == value.no && value2.taskname == value.taskname) {
                        value.status = value2.status;
                        value.date = value2.date;
                        value.remark = value2.remark;
                        value.actionby = value2.actionby;
                    }
                })
            })
            return vm.result;
        }
        vm.load();
        vm.changeList = [];

        function findElement(propValue) {
            for (var i = 0; i < vm.changeList.length; i++)
                if (vm.changeList[i]['id'] == propValue)
                    return i;

                // will return undefined if not found; you could return a default instead
        }
        // vm.calculate = function(ret) {
        //     if (vm.changeList.length < 1 || findElement(ret.id) == null) {
        //         vm.changeList.push(ret);
        //     } else {
        //         vm.changeList.splice(findElement(ret.id), 1)
        //         vm.changeList.push(ret);
        //     }
        // };


        vm.saveTable = function() {
            vm.changeList = [];
            vm.loading = true;
            var sendBody = {};
            sendBody.year = $window.sessionStorage.year;
            sendBody.month = $window.sessionStorage.month;
            sendBody.hotel = $window.sessionStorage.hotelId;
            sendBody.hotelName = vm.hotelName;
            sendBody.openDate = vm.opendate;
            if(vm.opendate==null || vm.opendate==''){
                vm.errmsg="Target Open Date is required.";
                return false;
            }
            sendBody.updateBy = vm.updateBy;
            vm.changeList = [];
            angular.forEach(vm.result, function(value, key) {
                if (value.date != null || value.status != null || value.remark != null || value.actionby != null) {
                    vm.changeList.push(value);
                }
            })
            sendBody.tasks = vm.changeList;

            PreopenService.taskPost(sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.msg = 'Saved. ' + new Date();
                    vm.load()
                }
            })

        }
    })
    .controller('PreopenRecruitCtrl', function($scope, $state, $http, $window, PreopenService) {
        var vm = this;
        vm.hotelId = $window.sessionStorage.hotelId;
        vm.hotelName = $window.sessionStorage.hotelName;
        vm.statusOptions = ['Completed', 'In Progress', 'Major Issues', 'Not Started Yet'];
        vm.typeOptions = ['Internal Transfer', 'External'];
        vm.phaseOptions = ['Candidate Identified', 'Confirmed', 'On-board']
        vm.editable = true;
        vm.opendate = new Date();
        vm.lastUpdateBy = 'Hotel';
        vm.loading = false;
        vm.loadData = function() {
            vm.loading = true;
            PreopenService.recruimentGet(vm.hotelId).then(function(ret) {
                vm.hotel = ret.data.value;
                vm.targetOpendate = new Date(vm.hotel.targetOpendate);
                vm.lastUpdated = vm.hotel.lastUpdateTime;
                vm.opendate = vm.hotel.openddate == null ? new Date() : new Date(vm.hotel.openddate);
                vm.result = vm.hotel.people;
                vm.loading = false;
            })
        }
        vm.loadData();


        vm.calculate = function() {};


        vm.saveTable = function() {
            vm.loading = true;
            var sendBody = vm.hotel;
            sendBody.year = $window.sessionStorage.year;
            sendBody.month = $window.sessionStorage.month;
            sendBody.hotelId = $window.sessionStorage.hotelId;
            sendBody.openddate = vm.opendate;
            sendBody.targetOpendate = vm.targetOpendate;

            PreopenService.recruimentPost(sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.msg = 'Saved. ' + new Date();
                    vm.loadData()
                }

            })

        }
    })
    .controller('TargetCtrl', function($scope, $state, $http, $window, PreopenService) {
        var vm = this;
        vm.hotelId = $window.sessionStorage.hotelId;
        vm.hotelName = $window.sessionStorage.hotelName;
        vm.statusOptions = ['Completed', 'In Progress', 'Major Issues', 'Not Started Yet'];
        vm.typeOptions = ['Internal Transfer', 'External'];
        vm.phaseOptions = ['Candidate Identified', 'Confirmed', 'On-board']
        vm.editable = true;
        vm.opendate = new Date();
        vm.loading = false;

        vm.loadData = function() {
            vm.total = [{
                no: '1.Brand Service Culture Program',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: '2.General Orientation',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: '3.Management Orientation',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: '4.Employee Program',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: '5.Supervisory Program',
                target: '70%',
                result: '',
                complete: true
            }, {
                no: '6.Management Program',
                target: '70%',
                result: '',
                complete: true
            }, {
                no: '7.Functional Training',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: '8.System Training / Vendor Training',
                target: '100%',
                result: '',
                complete: true
            }, {
                no: 'Final Status',
                target: '',
                result: '',
                complete: true
            }]
            vm.loading = true;
            PreopenService.targetGet(vm.hotelId).then(function(ret) {
                vm.result = ret.data.value;
                vm.brand = vm.result.brand;
                vm.gerneral = vm.result.gerneral;
                vm.managementO = vm.result.managementO;
                vm.employee = vm.result.employee;
                vm.supervisory = vm.result.supervisory;
                vm.managementP = vm.result.managementP;
                vm.functional = vm.result.functional;
                vm.system = vm.result.system;
                if (vm.brand[0].addDate != null) {
                    vm.lastUpdated = new Date(vm.brand[0].addDate);
                } else {
                    vm.lastUpdated = 'New';
                }

                vm.total[0].complete = vm.brand[0].complete;
                vm.total[0].result = vm.brand[0].result == null ? '' : vm.brand[0].result + '%';
                angular.forEach(vm.gerneral, function(value, key) {
                    vm.total[1].complete = vm.total[1].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[1].complete) {
                    vm.total[1].result = '100%';
                }
                angular.forEach(vm.managementO, function(value, key) {
                    vm.total[2].complete = vm.total[2].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[2].complete) {
                    vm.total[2].result = '100%';
                }
                angular.forEach(vm.employee, function(value, key) {
                    vm.total[3].complete = vm.total[3].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[3].complete) {
                    vm.total[3].result = '100%';
                }
                angular.forEach(vm.supervisory, function(value, key) {
                    vm.total[4].complete = vm.total[4].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[4].complete) {
                    vm.total[4].result = '100%';
                }
                angular.forEach(vm.managementP, function(value, key) {
                    vm.total[5].complete = vm.total[5].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[5].complete) {
                    vm.total[5].result = '100%';
                }
                angular.forEach(vm.functional, function(value, key) {
                    vm.total[6].complete = vm.total[6].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[6].complete) {
                    vm.total[6].result = '100%';
                }
                angular.forEach(vm.system, function(value, key) {
                    vm.total[7].complete = vm.total[7].complete && (value.complete == null ? false : value.complete);
                })
                if (vm.total[7].complete) {
                    vm.total[7].result = '100%';
                }
                angular.forEach(vm.total, function(value, key) {
                    vm.total[8].complete = vm.total[8].complete && value.complete;
                })
                
                vm.loading = false;
            })
        }
        vm.loadData();



        vm.calculate = function() {};


        vm.saveTable = function() {
            vm.loading = true;
            var sendBody = vm.result;
            sendBody.hotelId = $window.sessionStorage.hotelId;

            PreopenService.targetPost(sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.msg = 'Saved. ' + new Date();
                    vm.loadData()
                }

            })

        }
    })